import java.util.Arrays;
import java.util.Scanner;

/**
 * @Author Stark
 * @Date 2022/2/17 23:55
 */
public class Day8 {
    //1、字符串逆置，如"I am a student" 逆置为"student a am I" 。
    public static String reverse(String str){
        char[] arr = str.toCharArray();
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            char tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }
        return new String(arr);
    }
    public static String reverseSentence(String str) {
        str = reverse(str);
        int left = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i < str.length(); i++) {
            if(str.charAt(i) == ' '){
                sb.append(reverse(str.substring(left,i))).append(' ');
                left = i + 1;
            }
        }
        sb.append(reverse(str.substring(left,str.length()))).append(' ');
        return sb.toString();
    }

    //2、将用户输入的字符串转化（压缩）。
    //例如： "aabbccdaa" -> "a2b2c2d1a2" 或者 例如： "abbcccffr" -> "a1b2c3f2r1"
    public static String stringCompression(String string){
        StringBuilder sb = new StringBuilder();
        char ch = string.charAt(0);
        int count = 1;
        for (int i = 1 ; i < string.length(); i++) {
            if(ch != string.charAt(i)){
                sb.append(ch).append(count);
                ch = string.charAt(i);
                count = 1;
            }else {
                count++;
            }
        }
        sb.append(ch).append(count);
        return sb.toString();
    }
    public static void main(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        String string2 = scanner.nextLine();
//        System.out.println(reverseSentence(string2));

//        System.out.println(stringCompression("aabbccdaa"));
    }
}
